package com.lwj.project1;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.Toast;

import com.lwj.project1.PersonActivity.EditPersonInfoActivity;
import com.lwj.project1.PersonActivity.LikeActivity;
import com.lwj.project1.PersonActivity.ShowPersonInfoActivity;
import com.lwj.project1.PersonActivity.StoreHouseActivity;
import com.lwj.project1.databaseHelper.ImageUtil;
import com.lwj.project1.databaseHelper.MySqliteOpenHelper;
import com.lwj.project1.entity.User;
import com.lwj.project1.myToast.MyToast;


public class PersonFragment extends Fragment {
    private Button login_enter;
    private ImageView personHead;
    private ImageButton editPersonInfo;
    private ImageButton showPersonInfo;
    private ImageButton storeHouse;
    private ImageButton foot;
    private ImageButton rentRecord;
    private ImageButton like;
    private User user;
    MySqliteOpenHelper mySqliteOpenHelper;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_person, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view,@NonNull Bundle saveInstanceState){
        super.onViewCreated(view,saveInstanceState);
        SharedPreferences sharedPreferences = view.getContext().getSharedPreferences("project_content", Context.MODE_PRIVATE);
        MyToast myToast = new MyToast(getActivity().getApplicationContext());
        login_enter = view.findViewById(R.id.login_enter);
        personHead = view.findViewById(R.id.personhead);
        storeHouse = view.findViewById(R.id.storehouse);
        like = view.findViewById(R.id.like);
        foot = view.findViewById(R.id.foot);
        showPersonInfo = view.findViewById(R.id.showpersoninfo);
        rentRecord = view.findViewById(R.id.rentrecord);
        mySqliteOpenHelper = new MySqliteOpenHelper(getContext());
        editPersonInfo = view.findViewById(R.id.editpersoninfo);

        if (sharedPreferences.getString("state","").equals("LOGOUT")){
            login_enter.setText("LOG IN");
            personHead.setImageResource(R.drawable.head);
        }
        else {
            if (sharedPreferences.getString("state","").equals("LOGIN")){
                login_enter.setText("LOG OUT");
                String username = sharedPreferences.getString("username","");
                user = mySqliteOpenHelper.findUserByName(username);
                if (user != null && user.getHeadImgStr() != null){
                    personHead.setImageBitmap(ImageUtil.base64ToImage(user.getHeadImgStr()));
                }
            }
        }
        login_enter.setOnClickListener(v -> {
            if (sharedPreferences.getString("state","").equals("LOGIN")){
                myToast.showToastWithImg("退出登录成功哦~",getResources().getDrawable(R.drawable.toast_img));
                SharedPreferences.Editor editor = sharedPreferences.edit();
                editor.putString("state","LOGOUT");
                editor.apply();
                Intent intent = new Intent(view.getContext(),MainActivity.class);
                startActivity(intent);
            }
            Intent intent = new Intent(getActivity(),LoginActivity.class);
            startActivity(intent);
        });

        storeHouse.setOnClickListener(v -> {
            if (sharedPreferences.getString("state","LOGOUT").equals("LOGOUT")){
                myToast.showToastWithImg("请先登录哦~", getResources().getDrawable(R.drawable.toast_img));
                Intent intent = new Intent(view.getContext(),LoginActivity.class);
                startActivity(intent);
            }
            else {
                Intent intent = new Intent(view.getContext(), StoreHouseActivity.class);
                startActivity(intent);
            }
        });
        // TODO
        like.setOnClickListener(v -> {
            if (sharedPreferences.getString("state","LOGOUT").equals("LOGOUT")){
                myToast.showToastWithImg("请先登录哦~", getResources().getDrawable(R.drawable.toast_img));
                Intent intent = new Intent(view.getContext(),LoginActivity.class);
                startActivity(intent);
            }
            else {
                Intent intent = new Intent(view.getContext(), LikeActivity.class);
                startActivity(intent);
            }
        });
        foot.setOnClickListener(v -> {
            if (sharedPreferences.getString("state","LOGOUT").equals("LOGOUT")){
                myToast.showToastWithImg("请先登录哦~", getResources().getDrawable(R.drawable.toast_img));
                Intent intent = new Intent(view.getContext(),LoginActivity.class);
                startActivity(intent);
            }
            else {

            }

        });
        showPersonInfo.setOnClickListener(v -> {
            if (sharedPreferences.getString("state","LOGOUT").equals("LOGOUT")){
                myToast.showToastWithImg("请先登录哦~", getResources().getDrawable(R.drawable.toast_img));
                Intent intent = new Intent(view.getContext(),LoginActivity.class);
                startActivity(intent);
            }else {
                Intent intent = new Intent(view.getContext(), ShowPersonInfoActivity.class);
                startActivity(intent);
            }
        });
        rentRecord.setOnClickListener(v -> {
            if (sharedPreferences.getString("state","LOGOUT").equals("LOGOUT")){
                myToast.showToastWithImg("请先登录哦~", getResources().getDrawable(R.drawable.toast_img));
                Intent intent = new Intent(view.getContext(),LoginActivity.class);
                startActivity(intent);
            }

        });
        editPersonInfo.setOnClickListener(v -> {
            if (sharedPreferences.getString("state","LOGOUT").equals("LOGOUT")){
                myToast.showToastWithImg("请先登录哦~", getResources().getDrawable(R.drawable.toast_img));
                Intent intent = new Intent(view.getContext(),LoginActivity.class);
                startActivity(intent);
            }else {
                Intent intent = new Intent(view.getContext(), EditPersonInfoActivity.class);
                startActivity(intent);
            }
        });
    }
}